ADD_DEFINITIONS(-D__DOOM__ -D__DOOM_DLL__)

FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(GLUT REQUIRED)
FIND_PACKAGE(OpenAL REQUIRED)
FIND_PACKAGE(X11 REQUIRED)
FIND_PACKAGE(JPEG REQUIRED)

message(${OPENGL_INCLUDE_DIR})

INCLUDE_DIRECTORIES(${OPENAL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${X11_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})

SET(CM ../cm)
SET(FRAMEWORK ../framework)
SET(RENDERER ../renderer)
SET(SOUND ../sound)
SET(TOOLS ../tools)
SET(UI ../ui)
SET(JPEG ../jpeg)
SET(COMPILERS ${TOOLS}/compilers)
SET(GUIED ${TOOLS}/guied)
SET(DMAP ${COMPILERS}/dmap)
SET(AAS ${COMPILERS}/aas)
SET(ROQVQ ${COMPILERS}/roqvq)
SET(RENDERBUMP ${COMPILERS}/renderbump)

SET(SOURCES
    ${CM}/CollisionModel_contacts.cpp
    ${CM}/CollisionModel_contents.cpp
    ${CM}/CollisionModel_debug.cpp
    ${CM}/CollisionModel_files.cpp
    ${CM}/CollisionModel_load.cpp
    ${CM}/CollisionModel_rotate.cpp
    ${CM}/CollisionModel_trace.cpp
    ${CM}/CollisionModel_translate.cpp
    ${FRAMEWORK}/CmdSystem.cpp
    ${FRAMEWORK}/Common.cpp
    ${FRAMEWORK}/Compressor.cpp
    ${FRAMEWORK}/Console.cpp
    ${FRAMEWORK}/CVarSystem.cpp
    ${FRAMEWORK}/DeclAF.cpp
    ${FRAMEWORK}/DeclEntityDef.cpp
    ${FRAMEWORK}/DeclFX.cpp
    ${FRAMEWORK}/DeclManager.cpp
    ${FRAMEWORK}/DeclParticle.cpp
    ${FRAMEWORK}/DeclPDA.cpp
    ${FRAMEWORK}/DeclSkin.cpp
    ${FRAMEWORK}/DeclTable.cpp
    ${FRAMEWORK}/DemoFile.cpp
    ${FRAMEWORK}/EditField.cpp
    ${FRAMEWORK}/EventLoop.cpp
    ${FRAMEWORK}/File.cpp
    ${FRAMEWORK}/FileSystem.cpp
    ${FRAMEWORK}/KeyInput.cpp
    ${FRAMEWORK}/Session.cpp
    ${FRAMEWORK}/Session_menu.cpp
    ${FRAMEWORK}/Unzip.cpp
    ${FRAMEWORK}/UsercmdGen.cpp
    ${FRAMEWORK}/async/AsyncClient.cpp
    ${FRAMEWORK}/async/AsyncNetwork.cpp
    ${FRAMEWORK}/async/AsyncServer.cpp
    ${FRAMEWORK}/async/MsgChannel.cpp
    ${FRAMEWORK}/async/NetworkSystem.cpp
    ${FRAMEWORK}/async/ServerScan.cpp
    ${JPEG}/jmemdatasrc.c
    ${JPEG}/jmemdatasrc.h
    ${RENDERER}/Cinematic.cpp
    ${RENDERER}/draw_arb.cpp
    ${RENDERER}/draw_arb2.cpp
    ${RENDERER}/draw_common.cpp
    ${RENDERER}/draw_nv10.cpp
    ${RENDERER}/draw_nv20.cpp
    ${RENDERER}/draw_r200.cpp
    ${RENDERER}/GuiModel.cpp
    ${RENDERER}/Image_files.cpp
    ${RENDERER}/Image_init.cpp
    ${RENDERER}/Image_load.cpp
    ${RENDERER}/Image_process.cpp
    ${RENDERER}/Image_program.cpp
    ${RENDERER}/Interaction.cpp
    ${RENDERER}/Material.cpp
    ${RENDERER}/MegaTexture.cpp
    ${RENDERER}/Model.cpp
    ${RENDERER}/Model_ase.cpp
    ${RENDERER}/Model_beam.cpp
    ${RENDERER}/Model_liquid.cpp
    ${RENDERER}/Model_lwo.cpp
    ${RENDERER}/Model_ma.cpp
    ${RENDERER}/Model_md3.cpp
    ${RENDERER}/Model_md5.cpp
    ${RENDERER}/Model_prt.cpp
    ${RENDERER}/Model_sprite.cpp
    ${RENDERER}/ModelDecal.cpp
    ${RENDERER}/ModelManager.cpp
    ${RENDERER}/ModelOverlay.cpp
    ${RENDERER}/RenderEntity.cpp
    ${RENDERER}/RenderSystem.cpp
    ${RENDERER}/RenderSystem_init.cpp
    ${RENDERER}/RenderWorld.cpp
    ${RENDERER}/RenderWorld_demo.cpp
    ${RENDERER}/RenderWorld_load.cpp
    ${RENDERER}/RenderWorld_portals.cpp
    ${RENDERER}/tr_backend.cpp
    ${RENDERER}/tr_deform.cpp
    ${RENDERER}/tr_font.cpp
    ${RENDERER}/tr_guisurf.cpp
    ${RENDERER}/tr_light.cpp
    ${RENDERER}/tr_lightrun.cpp
    ${RENDERER}/tr_main.cpp
    ${RENDERER}/tr_orderIndexes.cpp
    ${RENDERER}/tr_polytope.cpp
    ${RENDERER}/tr_render.cpp
    ${RENDERER}/tr_rendertools.cpp
    ${RENDERER}/tr_shadowbounds.cpp
    ${RENDERER}/tr_stencilshadow.cpp
    ${RENDERER}/tr_subview.cpp
    ${RENDERER}/tr_trace.cpp
    ${RENDERER}/tr_trisurf.cpp
    ${RENDERER}/tr_turboshadow.cpp
    ${RENDERER}/VertexCache.cpp
    ${SOUND}/snd_cache.cpp
    ${SOUND}/snd_decoder.cpp
    ${SOUND}/snd_efxfile.cpp
    ${SOUND}/snd_emitter.cpp
    ${SOUND}/snd_shader.cpp
    ${SOUND}/snd_system.cpp
    ${SOUND}/snd_wavefile.cpp
    ${SOUND}/snd_world.cpp
    ${TOOLS}/edit_stub.cpp
    ${DMAP}/dmap.cpp
    ${DMAP}/facebsp.cpp
    ${DMAP}/gldraw.cpp
    ${DMAP}/glfile.cpp
    ${DMAP}/leakfile.cpp
    ${DMAP}/map.cpp
    ${DMAP}/optimize.cpp
    ${DMAP}/output.cpp
    ${DMAP}/portals.cpp
    ${DMAP}/shadowopt3.cpp
    ${DMAP}/tritjunction.cpp
    ${DMAP}/tritools.cpp
    ${DMAP}/ubrush.cpp
    ${DMAP}/usurface.cpp
    ${AAS}/AASBuild_file.cpp
    ${AAS}/AASBuild_gravity.cpp
    ${AAS}/AASBuild_ledge.cpp
    ${AAS}/AASBuild_merge.cpp
    ${AAS}/AASBuild.cpp
    ${AAS}/AASCluster.cpp
    ${AAS}/AASFile_optimize.cpp
    ${AAS}/AASFile_sample.cpp
    ${AAS}/AASFile.cpp
    ${AAS}/AASFileManager.cpp
    ${AAS}/AASReach.cpp
    ${AAS}/Brush.cpp
    ${AAS}/BrushBSP.cpp
    ${ROQVQ}/codec.cpp
    ${ROQVQ}/NSBitmapImageRep.cpp
    ${ROQVQ}/roq.cpp
    ${ROQVQ}/roqParam.cpp
    ${RENDERBUMP}/renderbump.cpp
    ${GUIED}/GEWindowWrapper_stub.cpp
    ${UI}/GameBearShootWindow.cpp
    ${UI}/GameBustOutWindow.cpp
    ${UI}/GameSSDWindow.cpp
    ${UI}/ListGUI.cpp
    ${UI}/BindWindow.cpp
    ${UI}/ChoiceWindow.cpp
    ${UI}/DeviceContext.cpp
    ${UI}/EditWindow.cpp
    ${UI}/FieldWindow.cpp
    ${UI}/GuiScript.cpp
    ${UI}/ListWindow.cpp
    ${UI}/MarkerWindow.cpp
    ${UI}/RegExp.cpp
    ${UI}/RenderWindow.cpp
    ${UI}/SimpleWindow.cpp
    ${UI}/SliderWindow.cpp
    ${UI}/UserInterface.cpp
    ${UI}/Window.cpp
    ${UI}/Winvar.cpp
    sys_local.cpp
)

IF (WIN32)
    SET(SOURCES
        ${SOURCES}
        win32/win_cpu.cpp
        win32/win_glimp.cpp
        win32/win_input.cpp
        win32/win_main.cpp
        win32/win_net.cpp
        win32/win_qgl.cpp
        win32/win_shared.cpp
        win32/win_snd.cp
    )
ENDIF (WIN32)

IF (UNIX)
    SET(SOURCES
        ${SOURCES}
        posix/posix_threads.cpp
        posix/posix_input.cpp
        posix/posix_main.cpp
        posix/posix_net.cpp
        posix/posix_signal.cpp
        linux/stack.cpp
        stub/util_stub.cpp
        )
ENDIF (UNIX)

IF (APPLE)
    SET(SOURCES
        ${SOURCES}
        osx/PreferencesDialog.cpp
        osx/PickMonitor.cpp
        osx/macosx_guids.cpp
        osx/macosx_sound.cpp
        osx/DOOMController.mm
        osx/macosx_event.mm
        osx/macosx_glimp.mm
        osx/macosx_misc.mm
        )

    ADD_DEFINITIONS(-mmacosx-version-min=10.6 -isysroot /Developer/SDKs/MacOSX10.6.sdk)

    SET(MACOSX_BUNDLE_EXECUTABLE_NAME "rr")
    SET(MACOSX_BUNDLE_INFO_STRING "Rogue Reborn")
    SET(MACOSX_BUNDLE_ICON_FILE "Doom3.icns")
    SET(MACOSX_BUNDLE_GUI_IDENTIFIER "rr")
    SET(MACOSX_BUNDLE_GUI_IDENTIFIER "com.roguereborn.rr")
    SET(APP_TYPE MACOSX_BUNDLE)
    ADD_EXECUTABLE(rr ${APP_TYPE} ${SOURCES})
    ADD_DEFINITIONS(-m32 -DID_OPENAL)

    add_custom_command(
        TARGET rr
        POST_BUILD
        COMMAND cp -rp ${CMAKE_CURRENT_SOURCE_DIR}/osx/Resources rr.app/Contents/
    )
    TARGET_LINK_LIBRARIES(rr idlib vorbis curl -m32 ${JPEG_LIBRARIES})
    TARGET_LINK_LIBRARIES(rr "-framework Carbon -framework OpenGL -framework IOKit -framework OpenAL -framework CoreAudio -framework Cocoa")
ENDIF (APPLE)

IF (FREEBSD)
    SET(SOURCES ${SOURCES} 
        linux/glimp.cpp
        linux/input.cpp
        posix/posix_input.cpp
        linux/libXNVCtrl/NVCtrl.c
        linux/main.cpp)
    ADD_EXECUTABLE(rr ${SOURCES})
    TARGET_LINK_LIBRARIES(rr idlib vorbis -m64 -L/usr/X11R6/lib -lX11 -lXext -lXxf86vm)
ENDIF (FREEBSD)
